package org.hyy.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.hyy.dao.CategoryDAO;
import org.hyy.dao.ProductDAO;
import org.hyy.vo.Category;
import org.hyy.vo.Product;

/**
 * 产品操作
 * 
 * @author yuanyuan.han
 * 
 */
public class ProductAction extends BasicAction {
	private static final long serialVersionUID = 286465596011139850L;
	private List<Category> categories;
	private List<Product> products;
	private String categoryString;
	
	public String getCategoryString() {
		return categoryString;
	}

	public List<Product> getProducts() {
		return products;
	}

	public List<Category> getCategories() {
		return categories;
	}

	public String showaddProduct() {
		// 声明
		CategoryDAO categoryDAO = null;

		categoryDAO = new CategoryDAO();
		categories = new ArrayList<Category>();

		categories = categoryDAO.GetCatergories();
		List<Category> list = new ArrayList<Category>();
		for (int i = 0; i < categories.size(); i++) {
			Category category = new Category();
			category = categories.get(i);
			// 第二层类别
			if (category.getGrade() == 2) {
				list.add(category);
			}
		}
		return "showadd";
	}

	public String addProduct(){
		HttpServletRequest request = getRequest();
		String name = request.getParameter("name");
		String descr = request.getParameter("descr");
		double normalPrice = Double.parseDouble(request.getParameter("normalPrice"));
		double memberPrice = Double.parseDouble(request.getParameter("memberPrice"));
		int categoryId = Integer.parseInt(request.getParameter("categoryId"));

		Product product = new Product();
		product.setName(name);
		product.setDescr(descr);
		product.setNormalPrice(normalPrice);
		product.setMemberPrice(memberPrice);
		product.setPdate(new Date());
		product.setCategoryId(categoryId);
		
		ProductDAO productDAO = new ProductDAO();
		productDAO.add(product);
		
		return "productlist";
	}
	
	public String delProduct(){
		HttpServletRequest request = getRequest();
		String pid = request.getParameter("pid");
		
		ProductDAO productDAO = new ProductDAO();
		productDAO.delet(Integer.valueOf(pid));
		
		return "productlist";
	}
	
	public String showProduct(){
		CategoryDAO categoryDAO = null;
		categoryDAO = new CategoryDAO();
		categories = new ArrayList<Category>();
		categories = categoryDAO.GetCatergories();
		HttpServletRequest request = getRequest();
		String cid = request.getParameter("cid");
		ProductDAO productDAO = new ProductDAO();
		products = productDAO.getProductByCid(Integer.valueOf(cid));
		for (Category category:categories) {
			if(Integer.valueOf(cid)==category.getId()){
				categoryString = category.getName();
			}
		}
		return "showproduct";
	}
}
